//
//  EditionTableViewController.m
//  bitcoin
//
//  Created by caiyajun on 2018/5/9.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "EditionTableViewController.h"
#import "EditionCell.h"
#import "EditionModel.h"
@interface EditionTableViewController ()
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation EditionTableViewController
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrowleft-icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem = back;
    
    [self requstData];
    
}
- (void)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)requstData{
    NSDictionary *param = @{@"type" : @"2",
                            };
    [LSRequestManager getRequestWithURL:HUOBI(@"api/pub/versionSpeech")
                                 params:param
                                success:^(NSDictionary *responseData) {
                                    if ([responseData[@"ret"] integerValue ] == 0) {
                                        NSArray *array = [EditionModel mj_objectArrayWithKeyValuesArray:responseData[@"data"]];
                                        [self.dataArray removeAllObjects];
                                        [self.dataArray addObjectsFromArray:array];
                                        [self.tableView reloadData];
                                    } else {
                                       [MBProgressHUD showError:responseData[@"msg"]];
                                    }
                                }
                                failure:^(NSError *error) {
                                    
                                }
                                showHUD:NO];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EditionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditionCell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"EditionCell" owner:nil options:nil] lastObject];
    }
    EditionModel*editionModel=self.dataArray[indexPath.row];
    cell.editionLabel.text=editionModel.title;
    if (editionModel.update_time.length>10) {
        cell.timeLabel.text=[editionModel.update_time substringToIndex:10];
    }else{
        cell.timeLabel.text=editionModel.update_time;
    }
    
    
//    NSString * htmlString;
//    if (indexPath.row==0) {
//        htmlString = @"<p>1.我的页面功能调整</p><p>2.首页部分样式更改</p><p>3.优化整体页面体验</p><p>4.修复一些崩溃问题</p><p>5.等等</p><p style=\"margin-top: 0px; margin-bottom: 0px; text-rendering: optimizeLegibility; font-feature-settings: \"kern\"; font-kerning: normal; color: rgb(153, 153, 153); font-family: \"微软雅黑 Regular\", 微软雅黑; font-size: 12px; white-space: normal; background-color: rgb(249, 249, 249);\"><br/></p>";
//    }else{
//        htmlString = @"<p>1.我的页面功能调整</p><p style=\"margin-top: 0px; margin-bottom: 0px; text-rendering: optimizeLegibility; font-feature-settings: \"kern\"; font-kerning: normal; color: rgb(153, 153, 153); font-family: \"微软雅黑 Regular\", 微软雅黑; font-size: 12px; white-space: normal; background-color: rgb(249, 249, 249);\"><br/></p>";
//    }
    
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[editionModel.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    [attrStr addAttribute:NSForegroundColorAttributeName value:RGBColor(153, 153, 153) range:NSMakeRange(0, attrStr.length)];
    cell.contentLabel.attributedText=attrStr;
    

    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 10;
}
@end
